package org.bmp.zemail.server.mail.controllers;

import org.bmp.zemail.server.common.models.Server;
import org.bmp.zemail.server.common.services.ExchangeMailService;
import org.bmp.zemail.server.common.services.ServerDao;
import org.bmp.zemail.server.folder.models.Folder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MailController {
	
	public final static String ROOT_MAPPING = "/server/mail/listAll";
	public final static String LIST_ALL_VIEW = "/server/mail/mailListAll";
	
	/*
	 * Spring injection
	 */
	@Autowired private ServerDao serverDao;
	public void setServerDao(ServerDao serverDao) {
		this.serverDao = serverDao;
	}
	@Autowired private ExchangeMailService exchangeMailService;
	public void setMailDao(ExchangeMailService exchangeMailService) {
		this.exchangeMailService = exchangeMailService;
	}
	
	@RequestMapping(MailController.ROOT_MAPPING)
	public @ResponseBody Folder listAll(@RequestParam String folder, @RequestParam String serverId,
			@RequestParam String action) {
		//get the server object
		Server server = serverDao.getServerById(serverId);
		
		//get mails for this server and folder
		exchangeMailService.setNextPage(server, folder, action);
		
		//return folder with mails
		return server.getFolder(folder);
	}
}
